<?php
class Cart extends AppModel{
    public $useTable  = 'cart';
    public $lang_code = 'vn';
    
    function get()
    {
        $lang_code = $this->lang_code;
        
        $sql = "SELECT cart.*, users.`name`, users.email, users.phone, custom.number_product, custom.sum_price, custom.sum_discount, cart_status.title
                FROM cart
                LEFT JOIN users ON cart.user_id = users.id
                LEFT JOIN cart_status ON cart.`status` = cart_status.`status` AND cart_status.lang_code = '$lang_code'
                LEFT JOIN
                (
                	SELECT cart_id, SUM(number) as number_product, SUM(price * number) AS sum_price, SUM(if(discount = 0, price, discount) * number) AS sum_discount
                	FROM cart_detail
                	GROUP BY cart_detail.cart_id
                ) custom ON cart.cart_id = custom.cart_id
                ORDER BY date_added DESC";
        $data = $this->query($sql);
        return $data;
        /*
        foreach($data as $value)
        {
            $temp = array();
            $temp['cart_id'] = $value['cart']['cart_id'];
            $temp['user_id'] = $value['cart']['user_id'];
            $temp['date_added'] = $value['cart']['date_added'];
            $temp['date_modified'] = $value['cart']['date_modified'];
            $temp['status'] = $value['cart']['status'];
            $temp['name'] = $value['users']['name'];
            $temp['email'] = $value['users']['email'];
            $temp['phone'] = $value['users']['phone'];
            $temp[number_product] => 8
            $temp[sum_price] => 1100000
            $temp[sum_discount] => 1050000
        }
        */
    }
    
    function get_cart_by_id($cart_id = null)
    {
        $sql = array(
			"conditions"=> array(
							"cart_id ="=> $cart_id,
						),
            "limit" => 1
			);
        $data = $this->find("all",$sql);
        if(!empty($data))
        {
            $data = $data[0];
        }
        return $data;
    }
    
    function get_detail_cart($cart_id = null)
    {
        $lang_code = $this->lang_code;
        $sql = "SELECT cart_detail.*, product_desc.title 
                FROM cart_detail
                LEFT JOIN product_desc ON cart_detail.product_id = product_desc.product_id AND product_desc.lang_code = '$lang_code'
                WHERE cart_id = '$cart_id'
                ORDER BY product_desc.title";
        $data = $this->query($sql);
        return $data;
    }
    
    function get_status_cart($cart_id = null)
    {
        $lang_code = $this->lang_code;
        $sql = "SELECT * FROM cart_status WHERE lang_code = '$lang_code'";
        $data = $this->query($sql);
        $html = "<select cart_id='$cart_id' class='form-control select_change_status_cart'><option value=''>Chọn trạng thái</option>";
        foreach($data as $value)
        {
            $status = $value['cart_status']['status'];
            $title = $value['cart_status']['title'];
            $html .= "<option value='$status'>$title</option>";
        }
        $html .= '</select><p class="close_change_status_cart">X
        </p>';
        return $html;
    }
    
    function change_status_cart($cart_id = null, $status = null)
    {
        $lang_code = $this->lang_code;
        $sql = "UPDATE cart SET `status` = '$status' WHERE cart_id = '$cart_id'";
        $this->query($sql);
        
        $sql = "SELECT title FROM cart_status WHERE `status` = $status AND lang_code = '$lang_code' LIMIT 1";
        $data = $this->query($sql);
        return $data[0]['cart_status']['title'];
    }
    
    function del($cart_id = "")
    {
    	$sql = "DELETE FROM cart WHERE cart_id = '$cart_id'";
    	$this->query($sql);
    	$sql = "DELETE FROM cart_detail WHERE cart_id = '$cart_id'";
    	$this->query($sql);
    	return 1;
    }
    
    function get_last_cart()
    {
        $sql = "SELECT cart.*, users.`name`, users.email, users.phone, custom.number_product, custom.sum_price, custom.sum_discount
                FROM cart
                LEFT JOIN users ON cart.user_id = users.id
                LEFT JOIN
                (
                	SELECT cart_id, SUM(number) as number_product, SUM(price * number) AS sum_price, SUM(if(discount = 0, price, discount) * number) AS sum_discount
                	FROM cart_detail
                	GROUP BY cart_detail.cart_id
                ) custom ON cart.cart_id = custom.cart_id
                
                ORDER BY date_added DESC
                LIMIT 10";
        return $this->query($sql);
    }
}
